//
//  TransViewController.m
//  MyFunctionDemo
//
//  Created by Ting Han on 2017/9/22.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "TransViewController.h"
#import "TransNextViewController.h"
#import "UIView+SetRect.h"
#import "ControllerPushAnimator.h"
#import "ControllerPopAnimator.h"
#import "PresentAnimator.h"
#import "DismissAnimator.h"

@interface TransViewController ()<UINavigationControllerDelegate,UIViewControllerTransitioningDelegate>
//交互式转场，只在当前控制器有效
@property (nonatomic, strong) UIPercentDrivenInteractiveTransition*
interactiveTransition;
@end

@implementation TransViewController
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.delegate = self;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if (self.navigationController.delegate == self) {
        self.navigationController.delegate = nil;
    }
    
}
- (void)setupDataSource{
    self.title = @"控制器的转场动画";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *array = @[@"push",@"present"];
    for (int i = 0; i<=1; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:button];
        button.backgroundColor = [[UIColor brownColor]colorWithAlphaComponent:0.3];
        button.tag = 10+i;
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.frame = CGRectMake(0, 0, kWidth(150), kWidth(80));
        [button addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        button.center = CGPointMake(self.contentView.centerX, kWidth(90)*(i+1));
    }
    //添加手势
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] init];
    [pan addTarget:self action:@selector(panGestureRecognizerAction:)];
    [self.contentView addGestureRecognizer:pan];
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, kWidth(300), kWidth(300), 0)];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.textColor = [UIColor brownColor];
    [self.contentView addSubview:label];
    label.text = @"当前页做了push和Pop的交互转场动画和present转场\n       pop转场动画，添加了交互式转场，采用UIPercentDrivenInteractiveTransition,配合自定义的转场动画,能实现手势滑动返回效果。\n        当自定义navigationBar的返回ButtonItem的时候，返回手势会失效，需要在navigationController中把self.navigationController.interactivePopGestureRecognizer.delegate = self设置成自己";
    [label sizeToFit];
    
}
- (void)didClickBtn:(UIButton *)sender{
    TransNextViewController *next = [[TransNextViewController alloc]init];
    switch (sender.tag) {
        case 10:
        {
            [self.navigationController pushViewController:next animated:YES];
        }
            break;
        case 11:
        {
            next.transitioningDelegate = self;
            [self presentViewController:next animated:YES completion:nil];
        }
            break;
        default:
            break;
    }
}
#pragma mark --- push&Pop
- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)pan{
    //产生百分比
    CGFloat process = [pan translationInView:self.view].x / ([UIScreen mainScreen].bounds.size.width);
    process = MIN(1.0,(MAX(0.0, process)));
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        self.interactiveTransition = [UIPercentDrivenInteractiveTransition new];
        //触发pop转场动画
        [self.navigationController popViewControllerAnimated:YES];
        
    }else if (pan.state == UIGestureRecognizerStateChanged){
        [self.interactiveTransition updateInteractiveTransition:process];
    }else if (pan.state == UIGestureRecognizerStateEnded
              || pan.state == UIGestureRecognizerStateCancelled){
        if (process > 0.5) {
            [ self.interactiveTransition finishInteractiveTransition];
        }else{
            [ self.interactiveTransition cancelInteractiveTransition];
        }
        self.interactiveTransition = nil;
    }
}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    if (operation == UINavigationControllerOperationPush) {
        
        return [ControllerPushAnimator new];
        
    } else if (operation == UINavigationControllerOperationPop) {
        
        return [ControllerPopAnimator new];
        
    } else {
        
        return nil;
    }
}
//交互转场
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController{
    if ([animationController isKindOfClass:[ControllerPopAnimator class]]) {
        return self.interactiveTransition;
    }
    return nil;
}
#pragma mark --- present &dismiss
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
   return  [[PresentAnimator alloc]init];
}
@end
